package module.map.propertys
{
	import drongo.model.core.IProperty;
	import drongo.model.events.ModelEvent;
	import drongo.model.values.CollectionValue;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import common.view.FGUIMediator;
	
	import module.map.Operations;
	import module.map.model.ProjectModel;
	import module.map.operations.propertys.datas.PropertyChangeData;
	
	import ui.Map.UI_NumberPropertyView;
	
	public class NumberPropertyViewMediator extends FGUIMediator
	{
		[Inject]
		public var view:UI_NumberPropertyView;
		[Inject]
		public var project:ProjectModel;
		
		private var data:CollectionValue;
		private var property:IProperty;
		public function NumberPropertyViewMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.data=this.view.data.data;
			this.property=this.view.data.property;
			this.AddEvents();
			this.RefreshView();
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function RefreshView():void{
			this.view.m_nameTxt.text=this.property.GetKey()+":";
			this.view.m_valueTxt.text=this.property.GetValue();
		}
		
		private function AddEvents():void{
			this.view.m_valueTxt.addEventListener(FocusEvent.FOCUS_OUT,this.TextChanged);
			this.property.addEventListener(ModelEvent.VALUE_CHANGED,this.PropertyUpdateHandler);
		}
		
		private function RemoveEvents():void{
			this.view.m_valueTxt.removeEventListener(FocusEvent.FOCUS_OUT,this.TextChanged);
			this.property.removeEventListener(ModelEvent.VALUE_CHANGED,this.PropertyUpdateHandler);
		}
		
		private function PropertyUpdateHandler(e:ModelEvent):void{
			this.RefreshView();
		}
		
		private function TextChanged(e:Event):void{
			var numValue:Number=Number(this.view.m_valueTxt.text);
			if(isNaN(numValue)){
				return;
			}
			var oldPropertys:Vector.<PropertyChangeData>=new Vector.<PropertyChangeData>();
			oldPropertys.push(new PropertyChangeData(this.property.GetKey(),this.property.GetValue()));
			
			var newPropertys:Vector.<PropertyChangeData>=new Vector.<PropertyChangeData>();
			newPropertys.push(new PropertyChangeData(this.property.GetKey(),numValue));
			
			project.undoSystem.executeOperation(Operations.COLLECTION_PROPERTYS_CHANGE,{parent:this.data,newPropertys:newPropertys,oldPropertys:oldPropertys});
		}
	}
}